package top.ljjapp.async;/**
 * @FileName: AsyncController.java
 * @Package: top.ljjapp.async
 * @Author: lijiajun
 * <br/>
 * @Created: 2018/11/21 15:21
 */

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.Callable;

/**
 * <p></p>
 * <p/>
 * <PRE>
 * <BR> 修改记录
 * <BR>-----------------------------------------------
 * <BR> 修改日期 修改人 修改内容
 * </PRE>
 *
 * @author lijiajun
 */
@RestController
@Slf4j
public class AsyncController {


    @Autowired
    private MockQueue mockQueue;

    @Autowired
    private DeferredResultHolder deferredResultHolder;


    @GetMapping("/order")
    public DeferredResult<String> order() throws Exception {
        log.info("主线程开始");

        String orderNumber = RandomStringUtils.randomNumeric(8);
        mockQueue.setPlaceOrder(orderNumber);

        DeferredResult<String> result = new DeferredResult<>();
        deferredResultHolder.getMap().put(orderNumber, result);

        return result;

//		Callable<String> result = new Callable<String>() {
//			@Override
//			public String call() throws Exception {
//				log.info("副线程开始");
//				Thread.sleep(1000);
//				log.info("副线程返回");
//				return "success";
//			}
//		};
    }
}